package club.kingyin.kyblog.core.extend;

import club.kingyin.kyblog.core.utils.cache.CacheAspect;
import club.kingyin.kyblog.core.utils.log.AbstractLogAspect;
import lombok.Data;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @author ：kingyin
 * @date ：创建于 2021/9/11 1:49 上午
 * @description ：
 * @modified By：
 * @version: 1.0.0
 */
@Aspect
@Component
public class LimitAspect extends AbstractLogAspect {

    // 辅助处理器：Redis
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Data
    class Handler {
        private RedisTemplate<String, String> redisTemplate;
    }

    @Pointcut("@annotation(club.kingyin.kyblog.core.extend.LogLimitConsole)")
    public void pt(){}

    @Around("pt()")
    public Object logDefault(ProceedingJoinPoint joinPoint) throws InstantiationException, IllegalAccessException {
        // 配置默认的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        // 传入切点和辅助处理器
        LimitAspect.Handler handler = new LimitAspect.Handler();
        handler.setRedisTemplate(redisTemplate);
        return log(joinPoint, handler);
    }
}
